package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.common.TokenHolder;
import com.itheima.domain.Category;
import com.itheima.exception.NoDataException;
import com.itheima.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    //新增分类
    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("----新增菜品分类-----");

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getName,category.getName());
        Category cag1 = categoryService.getOne(queryWrapper);
        //如果存在 抛出异常
        if(cag1!=null){
            log.info("输入的分类已存在");
            return  R.error("输入的分类已存在");
        }

        Category cag = new Category();
        //修改时间和操作者
        category.setCreateTime(LocalDateTime.now()+"");
        category.setUpdateTime(LocalDateTime.now()+"");
        category.setCreateUser(TokenHolder.getCurrentId());
        category.setUpdateUser(TokenHolder.getCurrentId());
        //新增
        assert false;
        BeanUtils.copyProperties(category,cag);
        categoryService.save(cag);
        return R.success("新增成功");
    }
    //分页查询
    @GetMapping("/page")
    public R<Page> pageSelect(int page ,int pageSize ,String name){
        log.info("--------分页查询开始-------");
        Page page1 = categoryService.pageSelect(page, pageSize, name);
        return R.success(page1) ;
    }
    //分类下拉列表   这里简单实现了全部数据的查询
    @GetMapping("/list")
    public R<List<Category>> catagroyPulldown(){
        log.info("------下拉分类----");
        List<Category> list = categoryService.list();
        return R.success(list);
    }
    @PutMapping
    public R<String> update(@RequestBody Category category ){
               log.info("修改分类信息:{}",category);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getId,category.getId());
        Category cag = categoryService.getOne(queryWrapper);
        if(cag==null){
            log.info("待修改数据不存在");
            throw  new NoDataException();
        }
        categoryService.updateById(category);

        return R.success("修改分类信息成功");
    }

    @DeleteMapping
    public R<String> delete(String id){
        log.info("---删除分类---");
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getId,id);
        Category cag = categoryService.getOne(queryWrapper);
        if(cag==null){
            log.info("要删除的数据不存在");
            throw  new  NoDataException();
        }
        if(cag.getType()==2){
            log.info(("套餐关联,删除失败"));
            return R.error("当前分类下关联了套餐，不能删除");
        }
        categoryService.removeById(id);
        return  R.success("删除成功");
    }

}
